<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>颜色切换-ref</title>
</head>

<body>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

    <div id="Application">
        <div class="demo">
            <button v-for="(item, index) in btns" :key="index" @click="onBtnClick(item.bgColor, item.textColor)">{{
                item.title }}</button>

            <div>
                <div class="example" ref="exeampleRef">Hello World</div>
            </div>
        </div>
    </div>
    <script>
        const App = Vue.createApp({
            setup() {
                const btns = [
                    { title: '红色主题', bgColor: '#FF9191', textColor: '#FF0000' },
                    { title: '蓝色主题', bgColor: '#B3C4FF', textColor: '#042BA9' },
                    { title: '默认主题', bgColor: '#333333', textColor: '#FFFFFF' }
                ]
                //拿到该div的引用
                const exeampleRef = Vue.ref()
                const onBtnClick = (bgColor, textColor) => {
                    console.log(bgColor, textColor, exeampleRef.value)
                    if (exeampleRef.value) {
                        exeampleRef.value?.style.setProperty('--textColor', textColor)
                        exeampleRef.value?.style.setProperty('--bgColor', bgColor)
                    }
                }

                return {
                    btns,
                    exeampleRef,
                    onBtnClick
                };
            },
        });
        App.mount("#Application");
    </script>
    <style scoped lang="scss">
        .demo {
            padding: 10px;
        }

        .demo .example {
            --textColor: #FFFFFF;
            --bgColor: #333333;

            display: inline-block;
            margin-top: 20px;
            font-size: 20px;
            padding: 20px 50px;
            color: var(--textColor);
            background: var(--bgColor);
        }
    </style>
</body>

</html>